package
{
	import flash.display.MovieClip;

	public class BounceBlockObject
	{
		public var name:String;
		public var xw:Number;
		public var yw:Number;
		public var pos:Vector2;
		public var oldpos:Vector2;
		public var anchor:Vector2;
		public var stiff:Number;
		public var mass:Number;
		public var ASLEEP:Boolean;
		public var sleepThreshold:Number;
		public var sleepTimer:Number;
		public var touchingObj:PlayerObject;
		public var mc:MovieClip;

		public function BounceBlockObject()
		{
			this.name = "bounce block";
			this.xw = Main.getInstance().tiles.xw * 0.8;
			this.yw = Main.getInstance().tiles.yw * 0.8;
			this.pos = new Vector2(10, 20);
			this.oldpos = new Vector2(30, 40);
			this.anchor = new Vector2(50, 60);
			this.stiff = 0.05;
			this.mass = 0.2;
			this.ASLEEP = true;
			this.sleepThreshold = 40;
			this.sleepTimer = 0;
			this.touchingObj = null;
			Main.getInstance().objects.Register(this);
			this.mc = Main.getInstance().gfx.CreateSprite("debugBounceBlockMC", NinjaGraphicsSystem.LAYER_OBJECTS);
			this.mc.visible = false;
		}

		public function Destruct():void
		{
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
		}

		public function Init(params:Array):void
		{
			if (params.length == 2)
			{
				this.pos.x = this.oldpos.x = this.anchor.x = params[0];
				this.pos.y = this.oldpos.y = this.anchor.y = params[1];
				this.mc.scaleX = 2 * this.xw;
				this.mc.scaleY = 2 * this.yw;
				this.Draw();
				this.mc.visible = true;
				Main.getInstance().objects.AddToGrid(this);
				Main.getInstance().objects.Moved(this);
			}
		}

		public function UnInit():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.EndDraw(this);
			Main.getInstance().objects.EndUpdate(this);
			Main.getInstance().objects.EndThink(this);
		}

		public function DumpInitData():String
		{
			return "" + this.anchor.x + NinjaGame.OBJPARAM_SEPERATION_CHAR + this.anchor.y;
		}

		public function IdleAfterDeath():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
		}

		public function Draw():void
		{
			this.mc.x = this.pos.x;
			this.mc.y = this.pos.y;
		}

		public function TestVsPlayer(guy:PlayerObject):void
		{
			var p = guy.pos;
			var dy = (p.y - this.pos.y);
			var _local2 = (this.yw + guy.yw) - Math.abs(dy);
			if (0 < _local2)
			{
				var dx = (p.x - this.pos.x);
				var _local3 = (this.xw + guy.xw) - Math.abs(dx);
				if (0 < _local3)
				{
					if (_local2 < _local3)
					{
						if (dy < 0)
						{
							var ny = -1;
							_local2 = _local2 * -1;
						}
						else
						{
							var ny = 1;
						}
						this.pos.y = this.pos.y - ((1 - this.mass) * _local2);
						guy.ReportCollisionVsObject(0, this.mass * _local2, 0, ny, this);
					}
					else
					{
						if (dx < 0)
						{
							_local3 = _local3 * -1;
							var nx = -1;
						}
						else
						{
							var nx = 1;
						}
						this.pos.x = this.pos.x - ((1 - this.mass) * _local3);
						guy.ReportCollisionVsObject(this.mass * _local3, 0, nx, 0, this);
					}
					this.sleepTimer = 0;
					if (this.ASLEEP)
					{
						this.Wake();
					}
					this.touchingObj = guy;
					return;
				}
			}
			else
			{
				this.touchingObj = null;
			}
		}

		public function Wake():void
		{
			Main.getInstance().objects.StartUpdate(this);
			Main.getInstance().objects.StartThink(this);
			Main.getInstance().objects.StartDraw(this);
			this.ASLEEP = false;
		}

		public function Sleep():void
		{
			Main.getInstance().objects.EndUpdate(this);
			Main.getInstance().objects.EndThink(this);
			Main.getInstance().objects.EndDraw(this);
			this.ASLEEP = true;
			this.oldpos.x = this.pos.x;
			this.oldpos.y = this.pos.y;
		}

		public function Think():void
		{
			if (this.sleepThreshold < this.sleepTimer)
			{
				this.Sleep();
			}
		}

		public function Update():void
		{
			var _local1 = this.pos;
			var _local3 = this.oldpos;
			var ox = _local3.x;
			var oy = _local3.y;
			var px = (_local3.x = _local1.x);
			var py = (_local3.y = _local1.y);
			_local1.x = _local1.x + (0.99 * (px - ox));
			_local1.y = _local1.y + (0.99 * (py - oy));
			var dx = (this.anchor.x - _local1.x);
			var dy = (this.anchor.y - _local1.y);
			if (0 < ((dx * dx) + (dy * dy)))
			{
				_local1.x = _local1.x + (dx * this.stiff);
				_local1.y = _local1.y + (dy * this.stiff);
				if (this.touchingObj != null)
				{
				}
			}
			this.sleepTimer++;
		}
	}
}